package com.my.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.my.pojo.Person;
import com.my.pojo.Vip;
import com.my.service.IVipService;
import com.my.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/vip")
public class VipController {
    IVipService vipService;

    @Autowired
    public void setVipService(IVipService vipService) {
        this.vipService = vipService;
    }

    RedisTemplate<String, String> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        jedisConnectionFactory.setDatabase(3);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        this.redisTemplate = redisTemplate;
    }

    private void putMessage(Map<String, String> map, String code, String message) {
        map.put("code", code);
        map.put("message", message);
    }

    private String VipToJSON(Vip vip) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("money", vip.getMoney());
        jsonObject.addProperty("level", vip.getStatus());
        return jsonObject.toString();
    }

    /**
     * 展示用户vip数据
     *
     * @param req header中存放用户token
     * @return
     *      500     token解析失败
     *      501     vip未创建
     *      200     成功
     *
     */
    @RequestMapping("/show")
    public String getVip(HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();

        Integer userId = JWTUtils.getId(req.getHeader("token"));
        if(userId == null) {
            putMessage(map, "500", "token解析失败");
        } else {
            Vip vip = vipService.getVip(userId);
            if(vip == null) {
                putMessage(map, "501", "vip未创建");
            } else {
                putMessage(map, "200", "成功");
                map.put("vip", VipToJSON(vip).toString());
            }
        }
        return new Gson().toJson(map);
    }

    /**
     * 创建vip
     *
     * @param req   header中存放用户token
     * @return
     *      500     token解析失败
     *      501     用户不存在
     *      502     已存在vip
     *      503     sql错误
     *      200     成功
     *
     */
    @RequestMapping("/create")
    public String createVip(HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();

        Integer userId = JWTUtils.getId(req.getHeader("token"));
        if(userId == null) {
            putMessage(map, "500", "token解析失败");
        } else {
            Integer res = vipService.createVip(userId);
            if(res == -1) {
                putMessage(map, "501", "用户不存在");
            } else if(res == -2) {
                putMessage(map, "502", "已存在vip");
            } else if(res == -3) {
                putMessage(map, "503", "sql错误");
            } else {
                putMessage(map, "200", "success");
            }
        }
        return new Gson().toJson(map);
    }

    /**
     * vip钱包充值
     *
     * @param count 充值金额
     * @param req   header中存放token
     * @return
     *      500     金额错误(<0)
     *      501     token解析失败
     *      502     用户不存在
     *      503     sql错误
     *      200     成功
     */
    @RequestMapping("/add")
    public String addMoney(Double count, HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();
        if(count <= 0) {
            putMessage(map, "500", "金额错误");
        } else {
            Integer userId = JWTUtils.getId(req.getHeader("token"));
            if(userId == null) {
                putMessage(map, "501", "token解析失败");
            } else {
                Integer res = vipService.addMoney(userId, count);
                if(res == -1) {
                    putMessage(map, "502", "用户不存在");
                } else if(res == -2) {
                    putMessage(map, "503", "未开通vip");
                } else if(res == -3) {
                    putMessage(map, "503", "sql错误");
                } else {
                    putMessage(map, "200", "success");
                }
            }
        }
        return new Gson().toJson(map);
    }


    /**
     * 减少金额
     *
     * @param count     金额数量
     * @param req       header中存放token
     * @return
     *      500     金额错误
     *      501     token解析失败
     *      502     用户不存在
     *      503     未开通vip
     *      504     余额不足
     *      505     sql错误
     *      200     成功
     *
     */
    @RequestMapping("/minus")
    public String minusMoney(Double count, HttpServletRequest req) {
        Map<String, String> map = new HashMap<>();
        if(count <= 0) {
            putMessage(map, "500", "金额错误");
        } else {
            Integer userId = JWTUtils.getId(req.getHeader("token"));
            if(userId == null) {
                putMessage(map, "501", "token解析失败");
            } else {
                Integer res = vipService.minusMoney(userId, count);
                if(res == -1) {
                    putMessage(map, "502", "用户不存在");
                } else if(res == -2) {
                    putMessage(map, "503", "未开通vip");
                } else if(res == -3) {
                    putMessage(map, "504", "余额不足");
                } else if(res == -4) {
                    putMessage(map, "505", "sql错误");
                } else {
                    putMessage(map, "200", "success");
                }
            }
        }
        return new Gson().toJson(map);
    }
}
